package com.rmkj.trade.match.core.ob;

import com.rmkj.trade.match.data.DataListener;
import com.rmkj.trade.match.util.MyLog;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * Created by Administrator on 2019/1/11.
 */
public abstract class ChangeObserver<T extends ChangeData> implements Observer {

	private final static MyLog _log = MyLog.getLog(ChangeObserver.class);

	private List<DataListener<T>> listeners = new ArrayList<>();

	public ChangeObserver(ChangeObservable changeObservable, Changeable<T> changeable) {
		super();
		changeObservable.setChangeable(changeable);
		changeObservable.addObserver(this);
	}

	@Override
	public void update(Observable o, Object arg) {
		ChangeObservable priceObservable = (ChangeObservable) o;
		final ChangeData data = priceObservable.getData();
//		_log.debug(data + "***************");

		// 同步处理
		listeners.forEach(it -> it.onChange((T)data));

//		_log.debug(data + "---------------");

	}

	public void addListener(DataListener dataListener){
		listeners.add(dataListener);
	}



}
